import 'weui'
require('../less/runing.less')
// 字体样式
require('../fonts/iconfont.css')

import weui from 'weui.js'

document.ready(function () {
    //加载导航守卫
    utils.navGuard()

    //跑步蒙层
    const runing_mask = document.querySelector('.runing_mask');
    // 获取蒙层数字
    let i = 3;
    const p = document.querySelector('.runing_mask p');
    const gps = document.querySelector('#gps');
    // 地图蒙层
    const runingmap = document.querySelector('.runingmap');
    const goback = document.querySelector('.goback');
    //显示时间
    const runing_time = document.querySelector('.runing_time');
    const runing_time1 = document.querySelector('.runing_time1');

    // 获取暂停继续结束
    const pause = document.querySelector('.pause');
    // 继续按钮
    const go = document.querySelector('.go');
    //结束按钮
    const end = document.querySelector('.end');
    //两个按钮盒子
    const Box = document.querySelector('.Box');

    //总路程
    const km_data = document.querySelector('.km_data');
    //配速
    const minkm = document.querySelector('.minkm');
    const minkm1 = document.querySelector('.minkm1');

    //总路程
    let ODO = 0;
    //配速
    let speed = 0;


    // 定义运动时间变量
    let sporttime = 0;

    let timer;

    function stime() {
        ++sporttime
        runing_time.textContent = utils.time(sporttime)
        runing_time1.textContent = utils.time(sporttime)
    }


    // 跑步蒙层倒计时
    let masktime = setInterval(() => {
        --i;
        if (i == 0) {
            p.textContent = "GO";
        } else if (i == -1) {
            clearInterval(masktime);
            runing_mask.style.display = 'none';

            // 开始计时
            timer = setInterval(() => {
                stime();
                getposition();
            }, 1000);
        } else {
            p.textContent = i
        }

    }, 1000);


    // 点击显示地图蒙层
    gps.addEventListener('click', function () {
        runingmap.style.display = 'block';
    });
    // 点击退出地图蒙层
    goback.addEventListener('click', function () {
        runingmap.style.display = 'none';
    });

    // 暂停运动
    pause.addEventListener('click', function () {
        this.style.visibility = 'hidden';
        Box.style.visibility = 'visible';

        // 清除定时器
        clearInterval(timer);
    });
    //继续运动
    go.addEventListener('click', function () {
        pause.style.visibility = 'visible';
        Box.style.visibility = 'hidden';
        timer = setInterval(() => {
            stime();
            getposition();
        }, 1000);
    });
    end.addEventListener('click', function () {
        clearInterval(timer);
        runingmap.style.display = 'block';
        goback.textContent = '完成';
        // 发送请求保存数据
        const data = {
            "id": localStorage.getItem('userId'),
            "type": 1,
            "takeTime": Math.ceil(sporttime/60),
            "miles": speed,
            "averagerate": 1,
            "calorie": 49
        }
        $http.post('/sports/save', data, function (res) {
            if (res.status == 0) {
                console.log(data);
                weui.toast('操作成功', {
                    duration: 3000,
                    callback: function () {
                        location.href = './run.html'
                    }
                });
            }
        })
    });

    var map = new BMap.Map("bdMap");
    // var point = new BMap.Point(114.324766,30.618342);
    // map.centerAndZoom(point, 15);
    let move = 0.001;
    let arr = [];

    function getposition() {
        // 初始化地图，设置中心点坐标和地图级别
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                move += 0.00001 * Math.floor(Math.random() * 10);
                let lng = r.longitude * 1 + move;
                let lat = r.latitude * 1 + move;
                arr.push({
                    lng: lng,
                    lat: lat
                })
                if (arr.length >= 2) {
                    let a = arr[arr.length - 2];
                    let b = arr[arr.length - 1];

                    let s = utils.distance(a, b);
                    ODO += s * 1;
                    km_data.textContent = ODO.toFixed(2);
                    console.log(ODO)

                    speed = (ODO * 1000) / (sporttime / 60);
                    minkm.textContent = speed.toFixed(2);
                    minkm1.textContent = speed.toFixed(2);
                    line(a, b);
                    arr.shift()
                }
                var point = new BMap.Point(lng, lat);
                map.centerAndZoom(point, 15);
            } else {
                console.log('failed' + this.getStatus());
            }
        });
    }
    getposition();

    function line(a, b) {
        var polyline = new BMap.Polyline([
            new BMap.Point(a.lng, a.lat),
            new BMap.Point(b.lng, b.lat)
        ], {
            strokeColor: "blue",
            strokeWeight: 6,
            strokeOpacity: 0.5
        });
        map.addOverlay(polyline);
    }





















})